package com.yz.platform.modules.biz.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yz.platform.common.utils.Constant;
import com.yz.platform.common.utils.PageUtils;
import com.yz.platform.common.utils.Query;
import com.yz.platform.common.utils.R;
import com.yz.platform.modules.biz.entity.ActivationCodeEntity;
import com.yz.platform.modules.biz.entity.ScriptEntity;
import com.yz.platform.modules.biz.service.ActivationCodeService;
import com.yz.platform.modules.biz.service.ScriptService;
import com.yz.platform.modules.sys.controller.AbstractController;
import com.yz.platform.modules.sys.entity.SysConfigEntity;
import com.yz.platform.modules.sys.entity.SysRoleEntity;
import com.yz.platform.modules.sys.entity.SysUserEntity;
import com.yz.platform.modules.sys.service.SysConfigService;
import com.yz.platform.modules.sys.service.SysRoleService;
import com.yz.platform.modules.sys.service.SysUserRoleService;
import com.yz.platform.modules.sys.service.SysUserService;


/**
 * 
 * 
 * @author lin
 * @email 
 * @date 2017-10-15 16:00:56
 */
@RestController
@RequestMapping("activationcode")
public class ActivationCodeController  extends AbstractController {

	@Autowired
	private ActivationCodeService activationCodeService;

	@Autowired
	private ScriptService scriptService;

	@Autowired
	private SysConfigService sysConfigService;

	@Autowired
	private SysUserService userService;

	@Autowired
	private SysRoleService roleService;

	@Autowired
	private SysUserRoleService userRoleService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("activationcode:list")
	public R list(@RequestParam Map<String, Object> params){

		List<Long> roleIds = userRoleService.queryRoleIdList(getUserId());

		for (Long roleId : roleIds) {

			if (roleId == 2){//普通用户只查询自己的
				params.put("userId",getUserId());
				break;
			}

			if (roleId == 3){//代理商能查自己创建的激活码
				params.put("createUser",getUserId());
				break;
			}

		}

        Query query = new Query(params);

		List<ActivationCodeEntity> activationCodeList = activationCodeService.queryList(query);

		int total = activationCodeService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(activationCodeList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{codeId}")
	@RequiresPermissions("activationcode:info")
	public R info(@PathVariable("codeId") Integer codeId){
		ActivationCodeEntity activationCode = activationCodeService.queryObject(codeId);
		
		return R.ok().put("activationCode", activationCode);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("activationcode:save")
	public R save(@RequestBody ActivationCodeEntity activationCode){
		activationCode.setCreateUser(getUserId().intValue());
		activationCode.setCreateTime(new Date());
		activationCodeService.save(activationCode);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("activationcode:update")
	public R update(@RequestBody ActivationCodeEntity activationCode){
		activationCodeService.update(activationCode);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("activationcode:delete")
	public R delete(@RequestBody Integer[] codeIds){
		activationCodeService.deleteBatch(codeIds);
		
		return R.ok();
	}


	/**
	 * 删除
	 */
	@RequestMapping("/stop")
	@RequiresPermissions("activationcode:delete")
	public R stop(@RequestBody Integer[] codeIds){
		activationCodeService.stopBatch(codeIds);
		return R.ok();
	}




	/**
	 * 脚本列表
	 */
	@RequestMapping("/select")
	@RequiresPermissions("activationcode:save")
	public R select(){

		Map<String, Object> map = new HashMap<>();

		List<ScriptEntity> list = scriptService.queryList(map);

		return R.ok().put("list", list);
	}


	/**
	 * 脚本列表
	 */
	@RequestMapping("/selectExpires")
	@RequiresPermissions("activationcode:save")
	public R selectExpires(){

		Map<String, Object> map = new HashMap<>();

		map.put("key",Constant.DICT_CODE_EXPIRES);

		List<SysConfigEntity> list = sysConfigService.queryList(map);

		return R.ok().put("list", list);
	}



	/**
	 * 用户列表
	 */
	@RequestMapping("/selectUsers")
	@RequiresPermissions("activationcode:save")
	public R selectUsers(){

		Map<String, Object> map = new HashMap<>();

		if(getUserId() != Constant.SUPER_ADMIN){
			map.put("createUserId", getUserId());
		}
		List<SysUserEntity> list = userService.queryList(map);

		return R.ok().put("list", list);
	}




}
